package com.shu.hb;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map.Entry;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shu.utils.EsUtils;
import com.shu.utils.FileUtils;

public class CheckHbMain {
	
	private static Long queryTotal(RestHighLevelClient client,String index,String scaleId) throws Exception {
		BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
		boolQuery.must(QueryBuilders.termQuery("scale_id", scaleId));
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.query(boolQuery);
		String[] indes = {index};
		SearchRequest searchRequest = new SearchRequest(indes,searchSourceBuilder);
		searchSourceBuilder.from(0);
		searchSourceBuilder.size(1);
		SearchResponse response = client.search(searchRequest);
		return response.getHits().getTotalHits();
	}
	
	
	private static void check( String[] indics, File writeFile,RestHighLevelClient client,String fileName) throws Exception  {
		ClassLoader classLoader = CheckHbMain.class.getClassLoader();  
        URL resource = classLoader.getResource(fileName);  
        String path = resource.getPath();  
        String json = readToString(path);
        ObjectMapper mapper = new ObjectMapper();  
        JsonNode readTree = mapper.readTree(json);
        json = readTree.get("data").toString();
        readTree = mapper.readTree(json);
        
        for(int i=0;i<readTree.size();i++) {
        	String scaleId = CheckHbMain.readJsonValue(mapper,readTree.get(i),"scaleId");
        	String content = scaleId+"|";
        	System.out.println(scaleId);
        	for(String index : indics) {
        		Long size = CheckHbMain.queryTotal(client, index, scaleId);
        		content += size+"|";
        	}
        	System.out.println(i+","+content);
        	FileUtils.writeFile(writeFile,content);
        }
	}

	public static void main(String[] args) throws Exception {
		ClassLoader classLoader = CheckHbMain.class.getClassLoader();  
        URL resource = classLoader.getResource("scale");  
        String path = resource.getPath();  
        File file = new File(path);
        String filePath = file.getParent()+File.separator+"result.txt";
        System.out.println(filePath);
        File writeFile = new File(filePath);
        String[] indics = {"scale_hb20180128","scale_hb20180129","scale_hb20180130","scale_hb20180131"};
        
        FileUtils.writeFile(writeFile,"index|"+Arrays.toString(indics));
        
        RestHighLevelClient client = EsUtils.getClient("127.0.0.1",9200); //主集群
        CheckHbMain.check(indics,writeFile, client, "scale");
        
    	client.close();
	}
	
	private static String readJsonValue(ObjectMapper objectMapper, JsonNode root,String key) {
		try {
		    Iterator<Entry<String, JsonNode>> elements = root.fields();
		      while (elements.hasNext()) {
		        Entry<String, JsonNode> node = elements.next();
		        if(key.equals(node.getKey())){
		        	return node.getValue().asText();
		        }
		      }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String readToString(String fileName) {  
        String encoding = "UTF-8";  
        File file = new File(fileName);  
        Long filelength = file.length();  
        byte[] filecontent = new byte[filelength.intValue()];  
        try {  
            FileInputStream in = new FileInputStream(file);  
            in.read(filecontent);  
            in.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        try {  
            return new String(filecontent, encoding);  
        } catch (UnsupportedEncodingException e) {  
            System.err.println("The OS does not support " + encoding);  
            e.printStackTrace();  
            return null;  
        }  
    }  
	
	

	
}
